package com.situ.mall.admin.controller;


import com.ramostear.captcha.HappyCaptcha;
import com.ramostear.captcha.support.CaptchaStyle;
import com.ramostear.captcha.support.CaptchaType;
import com.situ.mall.admin.util.ImageUtil;
import com.situ.mall.admin.util.RequestUtil;
import com.situ.mall.base.exception.InfoException;
import com.situ.mall.base.util.URLUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: CaptchaController
 * Package: com.situ.mall.controller
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-03-16 16:50
 * @Version 1.0
 */

@Controller
@RequestMapping("/")
public class CaptchaController {

    @GetMapping(URLUtil.CAPTCHA)
    public void happyCaptcha(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HappyCaptcha.require(req, resp).type(CaptchaType.DEFAULT).style(CaptchaStyle.ANIM).build().finish();
    }

    @PostMapping(URLUtil.VERIFY)
    public void verify(String code,HttpServletRequest req){
        if(!HappyCaptcha.verification(req,code,true)){
            throw new InfoException("验证码错误！", 500);
        }
    }

    @GetMapping(URLUtil.REMOVE + URLUtil.CAPTCHA)
    public void removeCaptcha(HttpServletRequest request){
        HappyCaptcha.remove(request);
    }


}
